package com.liwy3.dynamic;

import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * Copyright © 元气森林（北京）食品科技集团有限公司. All rights reserved.
 *
 * @Description:  整合druid详情见tax-admin项目
 * @author: Crazy4J
 * @date: 2021年11月17日 15:30
 * @version: V1.0
 */
@Configuration
public class DataSourceConfiguration {


    @Bean
    public DataSource prodDataSource() {
        DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("com.mysql.jdbc.Driver");
        dataSourceBuilder.url("jdbc:mysql://mysql.guzift.com:3307/tax_prod");
        dataSourceBuilder.username("root");
        dataSourceBuilder.password("Guzi1234");
        return dataSourceBuilder.build();
    }
    @Bean
    public DataSource testDataSource() {
        DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("com.mysql.jdbc.Driver");
        dataSourceBuilder.url("jdbc:mysql://mysql.guzift.com:3307/tax_test");
        dataSourceBuilder.username("root");
        dataSourceBuilder.password("Guzi1234");
        return dataSourceBuilder.build();
    }

    @Bean
    @Primary
    public DataSource dataSource() {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceType.MASTER.name(), prodDataSource());
        targetDataSources.put(DataSourceType.SLAVE.name(), testDataSource());
        return new DynamicDataSource(prodDataSource(), targetDataSources);
    }

}
